WeakMap and WeakSet hold weak references to their keys (and values in WeakMap), allowing objects to be garbage collected even while they remain in the collection, preventing memory leaks in caching, metadata, and DOM reference scenarios.
WeakMap and WeakSet are specialized collection types introduced in ES6 that enable memory-sensitive caching and metadata storage. Unlike Map and Set, which hold strong references and prevent garbage collection of their keys (and values in Map), WeakMap and WeakSet hold weak references. This means their presence alone does not keep objects alive. If an object used as a key (or value in WeakMap) becomes otherwise unreachable, the garbage collector can reclaim it, and the entry is automatically removed from the WeakMap/WeakSet. This behavior is crucial for preventing memory leaks in scenarios where you need to associate data with objects without interfering with their natural lifecycle.
Weak References: The references held by WeakMap keys and WeakSet values are weak, meaning they don't prevent garbage collection if no other strong references exist .
Non-Enumerable: WeakMap and WeakSet are not iterable. You cannot get a list of keys or values because that list could become outdated at any moment due to garbage collection .
Garbage Collection Timing: The removal of entries happens automatically during GC. You cannot observe or control exactly when it occurs .
Key Restrictions: WeakMap keys must be objects (not primitives) because primitives cannot be garbage collected in the same way . WeakSet values must also be objects .
Private Data Storage: Attaching private data to objects without exposing it publicly. The data is stored in a WeakMap keyed by the object, and when the object is destroyed, the private data goes with it .
DOM Element Metadata: Storing metadata or computed values for DOM elements without preventing their garbage collection when removed from the page .
Caching: Implementing caches that don't prevent cached objects from being collected when memory pressure is high .
Object Tagging: Marking objects with flags or timestamps without modifying the objects themselves or causing memory leaks .
Circular Reference Breaking: Weak references can help break cycles in complex object graphs, though this is less common .
It's important to understand that the 'weak' in WeakMap and WeakSet applies only to the keys (for WeakMap) and the values (for WeakSet). In WeakMap, the values can be strongly referenced and will keep objects alive if they themselves are objects. Similarly, iterating over a WeakMap is impossible because the list of keys could become invalid at any moment due to garbage collection—this is a deliberate design choice to maintain the integrity of the weak reference semantics.
Ephemeron Semantics: WeakMap implements 'ephemeron' semantics, meaning if a key is only reachable through its value (or values reachable from its value), both can be collected. This handles cases where key and value reference each other .
GC Integration: In V8, WeakMap entries are stored in special tables that the garbage collector scans but treats differently than regular object properties. The GC determines liveness of keys and removes entries during the marking phase .
Performance Trade-offs: WeakMap operations (get/set) are generally slower than Map because they require additional GC coordination and checks .
Memory Overhead: WeakMap has slightly higher memory overhead per entry due to the metadata needed for weak reference tracking .
The introduction of WeakMap and WeakSet represents a significant advancement in JavaScript's memory management capabilities. They enable patterns that were previously impossible without leaking memory or requiring manual cleanup. For library authors and application developers dealing with long-running applications, DOM manipulation, or extensive caching, these weak collection types are essential tools for building memory-efficient software. The key insight is that they allow the natural garbage collection of objects while still providing the ability to associate metadata, making them invaluable for scenarios where object lifetimes are unpredictable or outside your direct control.